﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Crawler_v1.utils
{
    class FileUtil
    {
        public static TextWriter Output { get; set; }

        /// <summary>
        /// 获取脚本
        /// </summary>
        /// <param name="scriptFile">脚本文件名</param>
        public static string GetScript(string scriptFile)
        {
            var scriptRoot = Path.Combine(Environment.CurrentDirectory,"scripts");
            string path = null;
            try
            {
                path = Path.Combine(scriptRoot, scriptFile);
                return File.ReadAllText(path);
            }
            catch (Exception ex)
            {
                Exception _e;
                do
                {
                    _e = ex;
                    ex = ex.InnerException;
                } while (ex != null);

                Output.WriteLine(_e.Message);
            }
            return null;
        }

        public static string readText(string filename)
        {
            //直接读取出字符串
            string text = System.IO.File.ReadAllText(filename);
            return text;
        }

        /*
        class ReadTextFile
        {
            static void Main()
            {
                //直接读取出字符串
                string text = System.IO.File.ReadAllText(@"C:\testDir\test1.txt");
                Console.WriteLine(text);

                //按行读取为字符串数组
                string[] lines = System.IO.File.ReadAllLines(@"C:\testDir\test.txt");
                foreach (string line in lines)
                {
                    Console.WriteLine(line);
                }

                //从头到尾以流的方式读出文本文件
                //该方法会一行一行读出文本
                using (System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\testDir\test.txt"))
                {
                    string str;
                    while ((str = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(str);
                    }
                }
                Console.Read();
            }
        }
         * */
    }
}
